/**
 * 
 */
package com.feib.stms.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import com.feib.stms.dao.TempDeliveryPaymentDao;
import com.feib.stms.model.ConfirmRule;
import com.feib.stms.model.PreDesignatedAccount;
import com.feib.stms.model.TempDeliveryPayment;
import com.feib.stms.model.TempUploadFile;
import com.feib.stms.model.UploadFile;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.DeliveryPaymentFileImportorService;
import com.feib.stms.service.FileImportorService;
import com.feib.stms.service.PreDesignatedAccountService;
import com.feib.stms.util.UserUtil;


/**
 * @title (#)Stms2001ActionBean.java<br>
 * @description 客戶交割款收付作業-交割款資料上傳 Controller<br/>
 * <ul>
 * <li>需求說明: 使用者上傳交割款資料。</li>
 * <li>業務規則說明:
 * <ol>
 * <li>上傳檔案為加密的檔案，系統必須解密。</li>
 * <li>上傳可以檔案上傳或自驗資檔案選擇上傳。</li>
 * </ol></li>
 * <li>使用單位及角色:券商端</li>
 * </ul>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2012/04/19
 * @copyright Far Eastern International Bank Copyright (c) 2012<br>
 * @2012/04/19 create by Jimmy Liu<br>
 */
public class Stms2001ActionBean extends BaseUploadActionBean 
{
	private static final long serialVersionUID = 2332490885791861424L;

	private static final String DEFAULT_JSP = "/WEB-INF/views/stms1001/stms1001.jsp";

	@SpringBean("deliveryPaymentFileImportor")
	private DeliveryPaymentFileImportorService deliveryPaymentFileImportor;

	public Stms2001ActionBean() {
		super();
		super.fileFormats = new ArrayList<String>();
		super.fileFormats.add(getUploadFileFormat());
	}

	@Override
	protected String getDefaultPage() {
		return DEFAULT_JSP;
	}

	@Override
	public String getUploadFileFormat() {
		return UploadFile.UPLOAD_FILE_DELIVERY_PAYMENT;
	}

	@Override
	public FileImportorService<TempDeliveryPaymentDao, TempDeliveryPayment> getFileImportorService() {
		return deliveryPaymentFileImportor;
	}

	@Override
	protected PaginatedList<TempUploadFile> invokeServiceToQuery(
			TempUploadFile tempUploadFile1, Date uploaddDateFrom1,
			Date uploadDateTo1, List<String> fileFormats1,
			PaginatedList<TempUploadFile> qryCondition) {

		return tempUploadFileService.findPaginatedListByFormatIs2(tempUploadFile1, uploaddDateFrom1, uploadDateTo1, qryCondition);
	}


	@SpringBean("preDesignatedAccountService")
	private PreDesignatedAccountService preDesignatedAccountService;


	/**
	 * 從TempUploadFile Table 複製到 UploadFile.
	 * 需發送Mail
	 * @return
	 */
	@Override
	public Resolution doUpload()
	{
		try {
			if (null != this.tempUploadFilePks)
			{
				ConfirmRule confirmRule = confirmRuleService.findConfirmRuleEnabled(UserUtil.getUserGroup());
				if (null == confirmRule)
				{
					this.errorMessage = messageSource.getMessage("unset.confirm.rule", null, "尚未設定券商流程！請先設定後再執行上傳！", this.getContext().getRequest().getLocale());
				}
				else
				{
					String noPreDesignatedAccountMsg = "尚未設定約定帳號！請先設定後再執行上傳！";
				
					for(int i=0; i < this.tempUploadFilePks.size(); i++)
					{
						Long pk = this.tempUploadFilePks.get(i);
						String  fileFormat =  this.tempUploadFileFormats.get(i);
						
						PreDesignatedAccount preDesignatedAccount=null;

						
						String bankBracnchNo=null;
						String account_No_H1=null;
						String account_No_P1=null;
						String account_No_R1=null;
											
						String account_No_H2=null;
						String account_No_P2=null;
						String account_No_R2=null;
						String preDesignatedAccountNo=null;
						String stockType=null;
						
						if (null != pk)
						{

							/* 如果有檔 取得約定帳號, 沒有的話要做處理 提示 類似劵商流程 */
							
							/* 驗資檔資料 */
							if (UploadFile.UPLOAD_FILE_DELIVERY_PAYMENT_CHK.equals(this.tempUploadFileFormats.get(i)))
							{
								UploadFile uploadFile = null;
								uploadFile=uploadFileService.findUploadFileByPk(pk);
								preDesignatedAccount = preDesignatedAccountService.getPreDesignatedAccountBySecuritiesNo(uploadFile.getSecuritiesNo(), uploadFile.getBankBracnchNo());							
								
								if (null == preDesignatedAccount){
									throw new Exception(noPreDesignatedAccountMsg);				// 未設定約定帳號
								}
								
								 /* 取得相關資訊  */
								 bankBracnchNo=uploadFile.getBankBracnchNo();
								 account_No_H1=preDesignatedAccount.getAccount_No_H1();
								 account_No_P1=preDesignatedAccount.getAccount_No_P1();
								 account_No_R1=preDesignatedAccount.getAccount_No_R1();
													
								 account_No_H2=preDesignatedAccount.getAccount_No_H2();
								 account_No_P2=preDesignatedAccount.getAccount_No_P2();
								 account_No_R2=preDesignatedAccount.getAccount_No_R2();
								 
								 stockType=uploadFile.getStockType().trim();
								
								
								
							
							}else{
								TempUploadFile tempUploadFile = null;
								tempUploadFile = tempUploadFileService.findTempUploadFileByPk(pk);
								
								preDesignatedAccount = preDesignatedAccountService.getPreDesignatedAccountBySecuritiesNo(tempUploadFile.getSecuritiesNo(), tempUploadFile.getBankBracnchNo());							
								
								if (null == preDesignatedAccount){
									throw new Exception(noPreDesignatedAccountMsg);				// 未設定約定帳號
								}
								
								 /* 取得相關資訊  */
								 bankBracnchNo=tempUploadFile.getBankBracnchNo();
								 account_No_H1=preDesignatedAccount.getAccount_No_H1();
								 account_No_P1=preDesignatedAccount.getAccount_No_P1();
								 account_No_R1=preDesignatedAccount.getAccount_No_R1();
													
								 account_No_H2=preDesignatedAccount.getAccount_No_H2();
								 account_No_P2=preDesignatedAccount.getAccount_No_P2();
								 account_No_R2=preDesignatedAccount.getAccount_No_R2();
								 
								 stockType=tempUploadFile.getStockType().trim();
																
							
							}

							
							if(preDesignatedAccount.getBankBracnchNo1().equals(bankBracnchNo)){		
								if ( "H".equals(stockType)){         
									preDesignatedAccountNo=account_No_H1;// 一般上市櫃
								}else if("P".equals(stockType)){	// 調撥
									preDesignatedAccountNo=account_No_P1;
								}else if("R".equals(stockType)){	// 興櫃
									preDesignatedAccountNo=account_No_R1;
								}
							}else if(preDesignatedAccount.getBankBracnchNo2().equals(bankBracnchNo)){
								if ("H".equals(stockType)){				// 一般上市櫃
									preDesignatedAccountNo=account_No_H2;
								}else if("P".equals(stockType)){		// 調撥
									preDesignatedAccountNo=account_No_P2;
								}else if("R".equals(stockType)){		// 興櫃
									preDesignatedAccountNo=account_No_R2;
								}
							}else{
								preDesignatedAccountNo=null;
							}

							if(null == preDesignatedAccountNo){
								throw new Exception(noPreDesignatedAccountMsg); // 未設定約定帳號
							}else{

								if (UploadFile.UPLOAD_FILE_DELIVERY_PAYMENT_CHK.equals(fileFormat)){// 驗資檔資料
									getFileImportorService().moveToUploadFile(pk, confirmRule.getCheckCount(), confirmRule.getConfirmCount(), true, preDesignatedAccountNo);
								}else{// 交割款資料
									getFileImportorService().moveToUploadFile(pk, confirmRule.getCheckCount(), confirmRule.getConfirmCount(), preDesignatedAccountNo);
								}
							}
						}
					}				

					this.message = messageSource.getMessage("upload.file.successed", null, "檔案上傳成功", this.getContext().getRequest().getLocale());
				}
			}
		} catch (Exception e) {
			logger.error("",e);

			if ("尚未設定約定帳號！請先設定後再執行上傳！".equals(e.getMessage())){
				this.errorMessage = messageSource.getMessage("unset.pre.designated.account", null, "尚未設定約定帳號！請先設定後再執行上傳！", this.getContext().getRequest().getLocale());
			}else{
				this.errorMessage = messageSource.getMessage("upload.file.exception", null, "上傳發生錯誤，請重新再試一次，若依就無法上傳，請聯絡網站管理人員，謝謝！", this.getContext().getRequest().getLocale());
			}
		}

		return this.doQuery(); 
	}


}
